package com.todo.strategy.online.application.handler.statusChange;

import com.alibaba.fastjson.JSONObject;
import com.todo.component.redis.RedisService;
import com.todo.strategy.common.constant.StrategyOnlineKeyBuild;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.PlanSetBO;
import com.todo.strategy.online.application.entity.bo.StrategyInfoBO;
import com.todo.strategy.online.application.entity.context.StrategyStatusContext;
import com.todo.strategy.online.application.service.PlanSetService;
import com.todo.strategy.online.application.service.StrategyInfoService;
import com.todo.strategy.online.infrastructure.helper.es.entity.BasicStrategyEntity;
import com.todo.strategy.online.infrastructure.helper.es.mapper.BasicStrategyEntityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/12/12 19:37
 * @description
 */
@Slf4j
@Component
public class PublishedHandler extends AbstractStrategyStatusHandler {

    @Resource
    private StrategyInfoService strategyInfoService;
    @Resource
    private RedisService redisService;
    @Resource
    private BasicStrategyEntityMapper strategyEntityMapper;
    @Resource
    private PlanSetService planSetService;


    @Override
    protected Boolean doStatusHandle(StrategyStatusContext context) {
        Long strategyId = context.getStrategyId();
        StrategyInfoBO strategyInfoBO = strategyInfoService.queryStrategyInfoById(strategyId);
        if (strategyInfoBO == null) {
            log.error("PublishedHandler#doStatusHandle 用户攻略发布状态处理完成，数据为空，默认执行成功 context:{}", JSONObject.toJSONString(context));
            return true;
        }
        if (!strategyInfoBO.getStatus().equals(context.getCurrentStatus())) {
            log.error("PublishedHandler#doStatusHandle 用户攻略当前数据状态与发送消息状态不一致，无法处理当前状态下的数据情况 context:{}，当前状态 :{}", JSONObject.toJSONString(context), strategyInfoBO.getStatus());
            return true;
        }
        PlanSetBO planSetBO = planSetService.queryPlanSetById(strategyInfoBO.getPlanId());
        if (planSetBO == null) {
            return false;
        }
        String detailKey = StrategyOnlineKeyBuild.strategyUserInfoKey(strategyId);
        redisService.set8H(detailKey, JSONObject.toJSONString(strategyInfoBO));

        //TODO 区分新增or更新
        BasicStrategyEntity entity = StrategyConvert.buildBasicStrategyEntity(strategyInfoBO, planSetBO);
        strategyEntityMapper.insert(entity);
        return true;
    }

    @Override
    protected boolean match(StrategyStatusContext context) {
        if (context == null || context.getStrategyId() == null || context.getCurrentStatus() == null) {
            log.error("PublishedHandler#match 用户攻略前置数据匹配失败，数据缺失或不存在 context:{}", JSONObject.toJSONString(context));
            return false;
        }
        return true;
    }

    @Override
    protected StrategyStatusEnum doGetDealStatus() {
        return StrategyStatusEnum.PUBLISHED;
    }
}
